import 'package:hi_common/hi_common.dart';
import 'package:json_annotation/json_annotation.dart';

part 'commission.g.dart';

@JsonSerializable()
class Commission extends Object {
  /*
    提成状态
   */
  static const String UN_BEGIN = 'unBegin';
  static const String BEGINNING = 'beginning';
  static const String END = 'end';

  /// 提成方式,原价百分比:oriPricePercent;售价百分比:salePricePercent;固定金额:fixed;
  /// 预订次数:bookNum;预订桌数:tableNum;预订人数:personNum;实付百分比:payAmount
  static const String ORI_PRICE_PERCENT = 'oriPricePercent';
  static const String SALE_PRICE_PERCENT = 'salePricePercent';
  static const String FIXED = 'fixed';
  static const String BOOK_NUM = 'bookNum';
  static const String TABLE_NUM = 'tableNum';
  static const String PERSON_NUM = 'personNum';
  static const String PAY_AMOUNT_PERCENT = 'payAmountPercent';

  /*
    提成类型
   */
  static const String COMMODITY = 'commodity';
  static const String BOOK = 'book';

  @JsonKey(name: 'beginDate')
  String beginDate;

  @JsonKey(name: 'beginTime')
  String beginTime;

  @JsonKey(name: 'commissionAmount')
  int commissionAmount;

  @JsonKey(name: 'commissionCommodityDOList')
  List<CommissionCommodityDOList> commissionCommodityDOList;

  @JsonKey(name: 'commissionName')
  String commissionName;

  @JsonKey(name: 'commissionTableDOList')
  List<CommissionTableDOList> commissionTableDOList;

  @JsonKey(name: 'commissionType')
  String commissionType;

  @JsonKey(name: 'commissionWaiterDOList')
  List<CommissionWaiterDOList> commissionWaiterDOList;

  @JsonKey(name: 'commissionWay')
  String commissionWay;

  @JsonKey(name: 'creator')
  int creator;

  @JsonKey(name: 'ctime')
  String ctime;

  @JsonKey(name: 'endDate')
  String endDate;

  @JsonKey(name: 'endTime')
  String endTime;

  @JsonKey(name: 'id')
  int id;

  @JsonKey(name: 'isDelete')
  String isDelete;

  @JsonKey(name: 'storeId')
  int storeId;

  @JsonKey(name: 'updater')
  int updater;

  @JsonKey(name: 'utime')
  String utime;

  @JsonKey(name: 'week')
  String week;

  @JsonKey(name: 'commodityInfos')
  List<CommodityInfo> commodityInfos;

  @JsonKey(name: 'tableInfos')
  List<TableInfo> tableInfos;

  @JsonKey(name: 'waiterInfos')
  List<WaiterInfo> waiterInfos;

  String get commodityNameText => commodityInfos.map((item) => '${item.title}${item.skuName == null ? '' : '(${item.skuName})'}').join(',');

  String get waiterNameText => waiterInfos.map((item) => '${item.realname}${item.rankName == null ? '' : '(${item.rankName})'}').join(',');

  String get tableNameText => tableInfos.map((item) => '${item.tableName}${item.areaName == null ? '' : '(${item.areaName})'}').join(',');

  String get status {
    final now = DateTime.now();

    if (beginDate == null || now.isAfter(DateTime.parse(beginDate)) && now.isBefore(DateTime.parse(endDate))) {
      return BEGINNING;
    } else if (now.isBefore(DateTime.parse(beginDate))) {
      return UN_BEGIN;
    } else {
      return END;
    }
  }

  bool get isFixed => [FIXED, BOOK_NUM, TABLE_NUM, PERSON_NUM].contains(commissionWay);

  String get amountText {
    if (isFixed) {
      return AmountUtil.price2String(commissionAmount);
    } else {
      try {
        final number = (commissionAmount / 100);
        return (NumberUtil.isInteger(number) ? number.toInt() : number).toString();
      } catch (e) {
        return null;
      }
    }
  }

  Commission(
    this.beginDate,
    this.beginTime,
    this.commissionAmount,
    this.commissionCommodityDOList,
    this.commissionName,
    this.commissionTableDOList,
    this.commissionType,
    this.commissionWaiterDOList,
    this.commissionWay,
    this.creator,
    this.ctime,
    this.endDate,
    this.endTime,
    this.id,
    this.isDelete,
    this.storeId,
    this.updater,
    this.utime,
    this.week,
  );

  factory Commission.fromJson(Map<String, dynamic> srcJson) => _$CommissionFromJson(srcJson);

  Map<String, dynamic> toJson() => _$CommissionToJson(this);
}

@JsonSerializable()
class CommissionCommodityDOList extends Object {
  @JsonKey(name: 'commissionRuleId')
  int commissionRuleId;

  @JsonKey(name: 'commodityId')
  int commodityId;

  @JsonKey(name: 'id')
  int id;

  @JsonKey(name: 'skuId')
  int skuId;

  CommissionCommodityDOList(
    this.commissionRuleId,
    this.commodityId,
    this.id,
    this.skuId,
  );

  factory CommissionCommodityDOList.fromJson(Map<String, dynamic> srcJson) => _$CommissionCommodityDOListFromJson(srcJson);

  Map<String, dynamic> toJson() => _$CommissionCommodityDOListToJson(this);
}

@JsonSerializable()
class CommissionTableDOList extends Object {
  @JsonKey(name: 'commissionRuleId')
  int commissionRuleId;

  @JsonKey(name: 'id')
  int id;

  @JsonKey(name: 'tableId')
  int tableId;

  CommissionTableDOList(
    this.commissionRuleId,
    this.id,
    this.tableId,
  );

  factory CommissionTableDOList.fromJson(Map<String, dynamic> srcJson) => _$CommissionTableDOListFromJson(srcJson);

  Map<String, dynamic> toJson() => _$CommissionTableDOListToJson(this);
}

@JsonSerializable()
class CommissionWaiterDOList extends Object {
  @JsonKey(name: 'commissionRuleId')
  int commissionRuleId;

  @JsonKey(name: 'id')
  int id;

  @JsonKey(name: 'waiterId')
  int waiterId;

  CommissionWaiterDOList(
    this.commissionRuleId,
    this.id,
    this.waiterId,
  );

  factory CommissionWaiterDOList.fromJson(Map<String, dynamic> srcJson) => _$CommissionWaiterDOListFromJson(srcJson);

  Map<String, dynamic> toJson() => _$CommissionWaiterDOListToJson(this);
}

@JsonSerializable()
class CommodityInfo {
  String skuName;
  String title;

  CommodityInfo(this.skuName, this.title);

  factory CommodityInfo.fromJson(Map<String, dynamic> srcJson) => _$CommodityInfoFromJson(srcJson);

  Map<String, dynamic> toJson() => _$CommodityInfoToJson(this);
}

@JsonSerializable()
class TableInfo {
  String areaName;
  String tableName;

  TableInfo(this.areaName, this.tableName);

  factory TableInfo.fromJson(Map<String, dynamic> srcJson) => _$TableInfoFromJson(srcJson);

  Map<String, dynamic> toJson() => _$TableInfoToJson(this);
}

@JsonSerializable()
class WaiterInfo {
  String rankName;
  String realname;

  WaiterInfo(this.rankName, this.realname);

  factory WaiterInfo.fromJson(Map<String, dynamic> srcJson) => _$WaiterInfoFromJson(srcJson);

  Map<String, dynamic> toJson() => _$WaiterInfoToJson(this);
}
